package buildinjava.io;

import com.google.common.base.Function;
import java.io.File;
import java.net.URISyntaxException;
import java.net.URL;

/**
 * URL utility functions.
 *
 * @author Gili Tzabari
 */
public final class Urls
{
	/**
	 * Prevent construction.
	 */
	private Urls()
	{
	}

	/**
	 * Returns a function that converts a URL to a File.
	 *
	 * The function throws IllegalArgumentException if the URL does not point to a File.
	 *
	 * @return a function that converts a URL to a File
	 */
	public static Function<URL, File> toFile()
	{
		return new Function<URL, File>()
		{
			@Override
			public File apply(URL from)
			{
				try
				{
					return new File(from.toURI());
				}
				catch (URISyntaxException e)
				{
					// Occurs if, for example, a user points to JAR file entry in classpath
					throw new IllegalArgumentException(e);
				}
			}
		};
	}
}
